package com.clsz.controller;

import com.clsz.bean.po.CinemaInfo;
import com.clsz.bean.po.FilmInfo;
import com.clsz.bean.po.WatchTimes;
import com.clsz.service.CinemaDetailService;
import com.clsz.service.FilmService;
import com.clsz.service.WatchTimesService;
import com.clsz.utils.RedisUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/watchTime")
public class WatchTimesController {
    @Resource
    private RedisUtils redisUtils;
    @Autowired
    private WatchTimesService watchTimesService;
    @Autowired
    private CinemaDetailService cinemaDetailService;
    @Autowired
    private FilmService filmService;

    /**
     * 根据场次编号
     * @param watchTimesId
     * @return
     */
    @GetMapping("/{watchTimesId}")
    public Object getOneWatchTime(@PathVariable("watchTimesId") String watchTimesId ,
                                  @RequestHeader String authorization) {
        Map<String, Object> res = new HashMap<>();
        //登录身份验证
        if (redisUtils.hasKey(authorization)) {
            WatchTimes watchTimes = watchTimesService.searchOne(watchTimesId);//场次信息
            CinemaInfo cinema = cinemaDetailService.getCinema(watchTimes.getCmaId());//影院信息
            FilmInfo filmInfo = filmService.searchOne(watchTimes.getFilmId());
            Map<String, Object> data = new HashMap<>();

            if (watchTimes != null) {
                data.put("watchTimes", watchTimes);
                data.put("cinema", cinema);
                data.put("film", filmInfo);
                res.put("code", "200");
                res.put("message", "ok");
                res.put("data", data);
            } else {
                res.put("code", "500");
                res.put("message", "资源不存在");
            }
        } else {
            res.put("code", "401");
            res.put("message", "身份验证失败");
            res.put("data", null);
        }
        return res;
    }
}
